//
//  GRReturnReasonView.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/24.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRReturnReasonView.h"
#import "GRReasonCell.h"
#import "RequestManager+GROrderManager.h"
#import "GRReturnResonModel.h"
@interface GRReturnReasonView()
@property (weak, nonatomic) IBOutlet UITableView *reasonList;

@property(nonatomic,weak)id <GRReturnReasonViewDelegate> delegate;
@property(nonatomic,assign)BOOL isReturnGood;
@property (weak, nonatomic) IBOutlet UIView *hiddenView;

@property(nonatomic,assign)BOOL isRiceOrder;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property(nonatomic,assign)NSInteger orderId;
@property(nonatomic,assign)BOOL isCancle;

@end
@implementation GRReturnReasonView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


-(NSMutableArray*)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}


+(instancetype)shareInstance{
    static GRReturnReasonView * returnGoodView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        returnGoodView = [GRReturnReasonView xx_instantiateFromNib];
        
    });
    return returnGoodView;
}
-(void)loadData{
    if (_isReturnGood) {
        self.titleLabel.text = @"退货原因";
    }else{
        self.titleLabel.text = @"退款原因";
    }
    [RequestManager getOrderReturnReson:^(BOOL success, id responseObject, GRError *error) {
        NSLog(@"%@",responseObject);
        if (success) {
            [self.dataSourceArr removeAllObjects];
            if (_isReturnGood) {
                NSArray * goodArr = responseObject[@"goods"];
                for (NSDictionary * payDic in goodArr) {
                    GRReturnResonModel * gooModel = [GRReturnResonModel yy_modelWithDictionary:payDic];
                    [self.dataSourceArr addObject:gooModel];
                }
            }else{
                NSArray * paydArr = responseObject[@"goods"];
                for (NSDictionary * payDic in paydArr) {
                    GRReturnResonModel * resonModel = [GRReturnResonModel yy_modelWithDictionary:payDic];
                    [self.dataSourceArr addObject:resonModel];
                }
            }
            [self.reasonList reloadData];
            [self.reasonList selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
      
    }];
    
}

+(void)showGoodView:(id <GRReturnReasonViewDelegate>) delegate  IsReturnGood:(BOOL)isReturnGood{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    GRReturnReasonView * resonView = [GRReturnReasonView shareInstance];
   resonView.delegate = delegate;
    resonView.isReturnGood = isReturnGood;
    [resonView loadData];

    [resonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(window);
    }];
}
+(void)showGoodView:(id <GRReturnReasonViewDelegate>) delegate  DataSource:(NSMutableArray *)dataSourceArr  OrderId:(NSInteger)orderId IsRiceOrder:(BOOL)isRiceOrder IsCancle:(BOOL)isCancle{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    GRReturnReasonView * resonView = [GRReturnReasonView shareInstance];
    resonView.titleLabel.text = isCancle? @"取消原因":@"退款原因";
    resonView.orderId = orderId;
    resonView.isRiceOrder = isRiceOrder;
    resonView.isCancle = isCancle;
    resonView.delegate = delegate;
    resonView.dataSourceArr = dataSourceArr;
    [resonView.reasonList reloadData];
    [resonView.reasonList selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
    [window addSubview:resonView];
    [window addSubview:resonView];
    [resonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(window);
    }];
    
}

-(void)awakeFromNib{
    [super awakeFromNib];
    self.reasonList.delegate = self;
    self.reasonList.dataSource = self;
    [self.reasonList registerNib:[UINib nibWithNibName:@"GRReasonCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"reasonCell"];
    self.reasonList.tableFooterView = [[UIView alloc]init];
    UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeAtion:)];
    [self.hiddenView addGestureRecognizer:tapGes];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GRReasonCell * resonCell = [tableView dequeueReusableCellWithIdentifier:@"reasonCell" forIndexPath:indexPath];
    if (_isRiceOrder) {
     resonCell.resonLabel.text = self.dataSourceArr[indexPath.row];
    }else{
        GRReturnResonModel * resonModel = self.dataSourceArr[indexPath.row];
        resonCell.resonLabel.text = resonModel.tex;
        
    }
    
    return resonCell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataSourceArr.count;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}


- (IBAction)submitAtion:(id)sender {
    NSIndexPath * seletedIndex = [self.reasonList indexPathForSelectedRow];
    [self removeFromSuperview];
    if (_isRiceOrder) {
        if (_delegate && [_delegate respondsToSelector:@selector(submitCancleReason:OrderId:sCancle:)]) {
            [_delegate submitCancleReason:self.dataSourceArr[seletedIndex.row] OrderId:_orderId sCancle:_isCancle];
        }
    }else{
    if (_delegate && [_delegate respondsToSelector:@selector(returnGoodReson:IsReturnGood:)]) {

        [_delegate returnGoodReson:self.dataSourceArr[seletedIndex.row] IsReturnGood:_isReturnGood ];
    }
    }
}



- (IBAction)closeAtion:(id)sender {
    [self removeFromSuperview];
}


@end
